<template>
	<jk-dialog
		v-model="showDialog"
		title="通知详情"
		width="800px"
		append-to-body
		:show-content-loading="showContentLoading"
		:show-cancel-btn="false"
		:show-close-btn="false"
		:show-confirm-btn="false"
		:confirm-btn-loading="confirmBtnLoading"
		@on-close="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
	>
		<div class="notice-title">{{ ruleForm.title }}</div>
		<div class="notice-sub-msg">
			<div class="margin-right-10">【{{ noticeTypeList.find(x => x.id === ruleForm.type) ? noticeTypeList.find(x => x.id === ruleForm.type).name : '' }}】</div>
			<div class="margin-right-10">{{ ruleForm.creatorName }}</div>
			<div>{{ ruleForm.createTime }}</div>
		</div>
		<el-input v-model="ruleForm.content" readonly type="textarea" :rows="16" placeholder="内容" />
		<el-form ref="ruleForm" class="margin-top-10" :model="ruleForm" :hide-required-asterisk="true" :show-message="false" label-width="110px" size="medium">
			<!--<el-row>
				<el-col :span="12">
					<el-form-item class="margin-bottom-0" label="通知主体">
						<span>{{ noticeTargetCategoryList.find(x => x.id === ruleForm.noticeObjectId) ? noticeTargetCategoryList.find(x => x.id === ruleForm.noticeObjectId).name : '' }}</span>
					</el-form-item>
				</el-col>
			</el-row>-->
			<el-row>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="生效时间">
						<span>{{ ruleForm.expiringTime }}</span>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="失效时间">
						<span>{{ ruleForm.failureTime }}</span>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<div class="flex-center">
			<el-button icon="el-icon-s-flag" type="primary" :loading="readBtnLoading" @click="onReadBtn">标为已读</el-button>
		</div>
	</jk-dialog>
</template>
<script>
    import { noticeTargetCategoryList, noticeTypeList } from '../../utils/jk/status-option-list';
    import { systemNoticeClearRead, systemNoticeGet } from '../../api/system/notice';

    export default {
        props: {
            dataId: {
                type: Number,
                default: null
            },
            dialogState: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                showContentLoading: false,
                confirmBtnLoading: false,
                noticeTypeList: noticeTypeList,
                noticeTargetCategoryList: noticeTargetCategoryList,
                targetList: [],
                workshopList: [],
                groupList: [],
                showDialog: false,
                ruleForm: {
                    'content': '',
                    'expiringTime': '',
                    'failureTime': '',
                    'ifTopped': false,
                    'noticeBeCurrentList': [],
                    'noticeObjectId': 0,
                    'sortNum': 1,
                    'title': '',
                    'type': 0,
                    'creatorName': ''
                },
                rules: {},
                readBtnLoading: false
            };
        },
        methods: {
            onReadBtn() {
                this.readBtnLoading = true;
                systemNoticeClearRead({
                    id: this.dataId
                }).then(res => {
                    this.readBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$emit('on-confirm');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                if (!this.dataId) return false;
                this.showContentLoading = true;
                return systemNoticeGet({ id: this.dataId }).then(res => {
                    this.showContentLoading = false;
                    if (res.data.status === 200) {
                        const resData = res.data.res || {};
                        resData.noticeBeCurrentList = resData.noticeBeCurrentList ? resData.noticeBeCurrentList.map(x => x.id) : [];
                        this.ruleForm = resData;
                    }
                });
            }
        },
        watch: {
            dialogState(newVal) {
                if (newVal) {
                    this.getDetailRequest();
                }
                this.showDialog = newVal;
            }
        }
    };
</script>
<style scoped lang="scss">
    .notice-title {
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 24px;
        font-weight: 500;
        height: 44px;
    }
    .notice-sub-msg {
        display: flex;
        justify-content: center;
        margin-bottom: 10px;
        font-size: 14px;
    }
</style>
